package com.whitewolf.gamer.controller;

import com.whitewolf.gamer.common.model.Result;
import com.whitewolf.gamer.service.UserService;
import com.whitewolf.gamer.vo.LoginResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResultVO> login(@RequestBody @Valid UserLoginDTO loginDTO) {
        LoginResultVO loginResult = userService.login(loginDTO);
        return Result.success(loginResult);
    }

    /**
     * 微信登录
     */
    @PostMapping("/wechat/login")
    public Result<LoginResultVO> wechatLogin(@RequestBody @Valid WechatLoginDTO wechatLoginDTO) {
        LoginResultVO loginResult = userService.wechatLogin(wechatLoginDTO);
        return Result.success(loginResult);
    }

    /**
     * 刷新令牌
     */
    @PostMapping("/refresh")
    public Result<String> refreshToken(@RequestParam String refreshToken) {
        String token = userService.refreshToken(refreshToken);
        return Result.success(token);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        // 由于JWT无状态，客户端只需删除本地令牌即可
        return Result.success();
    }
}